package com.cppsu.cgx.libraryseatselection.controller;

import com.cppsu.cgx.libraryseatselection.common.api.CommonResult;
import com.cppsu.cgx.libraryseatselection.dto.AdminParam;
import com.cppsu.cgx.libraryseatselection.dto.UserLoginParam;
import com.cppsu.cgx.libraryseatselection.services.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 9:38 2021/3/12
 * @Modified By:
 */
@Controller
@Api(tags = "管理员管理")
@RequestMapping("/admin")
@CrossOrigin(origins = "*",maxAge = 3600)
public class AdminController {
    @Autowired
    AdminService adminService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody @Valid AdminParam adminParam) {
        String token = adminService.login(adminParam);
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
}
